class ProductOfNumbers {
    List<Integer> prefixProduct;
    int product;
    int lastZeroIdx;

    public ProductOfNumbers() {
        prefixProduct = new ArrayList<>();
        product = 1;
        lastZeroIdx = -1;
        
    }
    
    public void add(int num) {
        if(num == 0) {
            product = 1;
            lastZeroIdx = prefixProduct.size();
        } else {
            product *= num;
        }


        prefixProduct.add(product);
        
    }
    
    public int getProduct(int k) {
        int n = prefixProduct.size();

        if(lastZeroIdx >= n - k) return 0;

        if(k == n) return product;
        else
            return prefixProduct.get(n-1) / prefixProduct.get(n - k - 1);

    }
}

/**
 * Your ProductOfNumbers object will be instantiated and called as such:
 * ProductOfNumbers obj = new ProductOfNumbers();
 * obj.add(num);
 * int param_2 = obj.getProduct(k);
 */